import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:fluttertoast/fluttertoast.dart';
import '../../entity/news_list_entity.dart';
import '../../util/SignUtil.dart';
import '../../base/Constant.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../http/Api.dart';
import '../../http/HttpUtil.dart';
import 'ArticleDetail.dart';

class InformNewsPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return InformNewsView();
  }
}

class InformNewsView extends State<InformNewsPage> {
  List<HomeListDataData> articles = new List();
  EasyRefreshController _controller = new EasyRefreshController();
  int page = 1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadList();
  }

  loadList() async {
    Map map = new Map();
    map['cat_id'] = "4";
    map['p'] = page.toString();
    map['per'] = "10";
    map['sign_type']=createSignType(Api.URL_ARTICLE_LIST);
    Response<String> response =
        await HttpUtil().post(Api.URL_ARTICLE_LIST, data: getSign(map));
    print(response.data);
    Map userMap = json.decode(response.data);
    HomeListEntity homeServiceEntity = new HomeListEntity.fromJson(userMap);
    if (userMap['code'] == 1 && mounted) {
      if (page == 1) {
        articles.clear();
        _controller.resetLoadState();
        _controller.finishRefresh();
      } else {
        _controller.finishLoad(noMore: homeServiceEntity.data.length==0);
        if(homeServiceEntity.data.length==0){
          Fluttertoast.showToast(msg: "已经加载全部");
        }
      }
      articles.addAll(homeServiceEntity.data);
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          "通知消息",
          style: TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
              color: Colors.black),
        ),
        elevation: 0.5,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
      ),
      body: new Container(
        color: divideColor,
        child: new EasyRefresh(
          enableControlFinishLoad: true,
          enableControlFinishRefresh: true,
          header: MaterialHeader(),
          footer: MaterialFooter(),
          controller: _controller,
          child: ListView.separated(
              itemBuilder: _renderRow,
              separatorBuilder: (BuildContext context, int index) {
                return Container(
                  color: divideColor,
                  height: 1,
                );
              },
              itemCount: articles.length),
          onRefresh: () async {
            page = 1;
            loadList();
          },
          onLoad: () async {
            page++;
            loadList();
          },
        ),
      ),
    );
  }

  Widget _renderRow(BuildContext context, int index) {
    return new GestureDetector(
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (context) {
          return new ArticleDetailPage(
            article_id: articles[index].article_id,
          );
        }));
      },
      child: new Container(
        color: Colors.white,
        padding: EdgeInsets.all(15.0),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            new Row(
              children: <Widget>[
                Expanded(
                  child: Text(
                    articles[index].title,
                    style: TextStyle(
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                        color: Colors.black),
                  ),
                ),
                Text(
                  articles[index].pubtime,
                  style: TextStyle(
                      fontSize: ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                      color: Colors.black54),
                )
              ],
            ),
            Container(
              padding: EdgeInsets.fromLTRB(0, 5, 0, 0),
              child: Text(
                articles[index].description,
                style: TextStyle(
                    fontSize: ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                    color: Colors.black54),
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
